package com.tl.wechat.pay.utils;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;

/**
 * 
 * @author 15110
 *
 */
public class WXPayConfig {
	
	private byte[] certData;
	
	/**
	 * 
	 */
	private String appID;
	
	/**
	 * 
	 */
    private String appSecret;
    
    /**
     * 商户id
     */
    private String mchID;
    
    private String key;
    
    private String serialNO;
    
    private String certStreamPath;
    
    private InputStream certStream;
    
    private String certStreamKey;
    
    /**
     * 回调地址
     */
    private String payCallBackUrl;
    
    /**
     * 退款回调地址
     */
    private String refundCallBackUrl;
    
    /**
     * 客户端ip
     */
    private String cliIP;
    
    public String getAppID() {
		return appID;
	}

	public void setAppID(String appID) {
		this.appID = appID;
	}

	public String getAppSecret() {
		return appSecret;
	}

	public void setAppSecret(String appSecret) {
		this.appSecret = appSecret;
	}

	public String getMchID() {
		return mchID;
	}

	public void setMchID(String mchID) {
		this.mchID = mchID;
	}

	public String getKey() {
		return key;
	}

	public void setKey(String key) {
		this.key = key;
	}

	public String getSerialNO() {
		return serialNO;
	}

	public void setSerialNO(String serialNO) {
		this.serialNO = serialNO;
	}

	public String getCertStreamPath() {
		return certStreamPath;
	}

	public void setCertStreamPath(String certStreamPath) {
		this.certStreamPath = certStreamPath;
	}

	public InputStream getCertStream() {
        try {
            File file = new File(certStreamPath);
            InputStream certStream = new FileInputStream(file);
            this.certData = new byte[(int) file.length()];
            certStream.read(this.certData);
            certStream.close();
        } catch (Exception e) {
            System.out.println("加载微信证书文件失败------------》");
        }
        ByteArrayInputStream certBis = new ByteArrayInputStream(this.certData);
        return certBis;
    }

	public void setCertStream(InputStream certStream) {
		this.certStream = certStream;
	}

	public String getPayCallBackUrl() {
		return payCallBackUrl;
	}

	public void setPayCallBackUrl(String payCallBackUrl) {
		this.payCallBackUrl = payCallBackUrl;
	}

	public String getRefundCallBackUrl() {
		return refundCallBackUrl;
	}

	public void setRefundCallBackUrl(String refundCallBackUrl) {
		this.refundCallBackUrl = refundCallBackUrl;
	}

	public String getCliIP() {
		return cliIP;
	}

	public void setCliIP(String cliIP) {
		this.cliIP = cliIP;
	}

	public String getCertStreamKey() {
		return certStreamKey;
	}

	public void setCertStreamKey(String certStreamKey) {
		this.certStreamKey = certStreamKey;
	}

	/**
     * HTTP(S) 连接超时时间，单位毫秒
     *
     * @return
     */
    public int getHttpConnectTimeoutMs() {
        return 6*1000;
    }

    /**
     * HTTP(S) 读数据超时时间，单位毫秒
     *
     * @return
     */
    public int getHttpReadTimeoutMs() {
        return 8*1000;
    }


    /**
     * 是否自动上报。
     * 若要关闭自动上报，子类中实现该函数返回 false 即可。
     *
     * @return
     */
    public boolean shouldAutoReport() {
        return true;
    }

    /**
     * 进行健康上报的线程的数量
     *
     * @return
     */
    public int getReportWorkerNum() {
        return 6;
    }


    /**
     * 健康上报缓存消息的最大数量。会有线程去独立上报
     * 粗略计算：加入一条消息200B，10000消息占用空间 2000 KB，约为2MB，可以接受
     *
     * @return
     */
    public int getReportQueueMaxSize() {
        return 10000;
    }

    /**
     * 批量上报，一次最多上报多个数据
     *
     * @return
     */
    public int getReportBatchSize() {
        return 10;
    }
    
    public IWXPayDomain getWXPayDomain() {
        // 这个方法需要这样实现, 否则无法正常初始化WXPay
        IWXPayDomain iwxPayDomain = new IWXPayDomain() {
            public void report(String domain, long elapsedTimeMillis, Exception ex) {
            }
            public DomainInfo getDomain(WXPayConfig config) {
                return new IWXPayDomain.DomainInfo(WXPayConstants.DOMAIN_API, true);
            }
        };
        return iwxPayDomain;

    }

}
